package com.example.f8399.testdemo.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.adapter.ExpandableListadapter;
import com.example.f8399.testdemo.bean.ExpandLev1;
import com.example.f8399.testdemo.bean.ExpandLev2;
import com.example.f8399.testdemo.databinding.FragmentFirstBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 这个界面用两个功能：
 * 第一个是在viewpager中做切换使用的
 * 第二个就是展示树形列表的功能 ：https://github.com/CymChad/BaseRecyclerViewAdapterHelper
 * 这里有个官方的坑，那就是我使用了带有alpha1 标志的2.9.43-alpha1 版本，
 * 无法使用，因为包里未导入recyclerview这个框架，让我调试了半天，改用2.9.42就好了
 */
public class FirstFragment extends Fragment {


    private View view;
    FragmentFirstBinding mBinding;
    private ExpandableListadapter mAdapter;

    public static FirstFragment newIntants() {
        FirstFragment fragment = new FirstFragment();
        Bundle bundle = new Bundle();
        bundle.putString("msg", "hello");
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.d("FirstFragment is created ....");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {//md 这个我地方我居然之前写了！=，草了，让我调试了半天
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_first, container, false);
            view = mBinding.getRoot();
            Bundle arguments = getArguments();
            if (arguments != null) {
                String msg = arguments.getString("msg");
                if (!TextUtils.isEmpty(msg)) {
                    mBinding.tvMsg.setText(msg);
                }
            }

            List<MultiItemEntity> list = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                ExpandLev1 e = new ExpandLev1();
                e.setTitle("Title" + i);
                for (int i1 = 0; i1 < 6; i1++) {
                    ExpandLev2 expandLev2 = new ExpandLev2();
                    expandLev2.setMsg("msg" + i1);
                    e.addSubItem(expandLev2);
                }
                list.add(e);
            }
            mAdapter = new ExpandableListadapter(list);
            LinearLayoutManager layout = new LinearLayoutManager(getActivity());
            mBinding.rc.setAdapter(mAdapter);
            mBinding.rc.setLayoutManager(layout);
            mBinding.rc.addItemDecoration(new DividerItemDecoration(getActivity(), layout.getOrientation()));
            mAdapter.expandAll();
        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LogUtils.d("here is onViewCreated after onCreateView");
    }


}
